package net.bblot.lottery.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.Window;

import com.igexin.sdk.PushManager;

import net.bblot.lottery.PackegeConfig;
import com.marsscp.cp.R;
import net.bblot.lottery.util.AppConfig;
import net.bblot.lottery.util.Logger;

public class LogoActivity extends BaseActivity {

    private final static int REQ_CODE_PERMISSION_INTERNET = 101;
    private final static int REQ_CODE_PERMISSION_LOCATION = 102;
    private final static int REQ_CODE_PERMISSION_STORAGE = 103;
    private final static int REQ_CODE_PERMISSION_PHONE = 104;
    private final static String TAG = "LogoActivity";

    // private AnimationDrawable animationDrawable;

    Handler mHandler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Logger.d(TAG, "onCreate");
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= 23) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
                String[] strPermissions = new String[]{Manifest.permission.INTERNET};
                ActivityCompat.requestPermissions(this, strPermissions, REQ_CODE_PERMISSION_INTERNET);
            } else {
                start();
            }
        } else {
            start();
        }

//		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//				WindowManager.LayoutParams.FLAG_FULLSCREEN);
//		setContentView(R.layout.activity_logo);
//		mHandler.postDelayed(new Runnable() {
//			@Override
//			public void run() {
//				// TODO Auto-generated method stub
//				AppManager.getAppManager().finishActivity(LogoActivity.this);
//				 
//				AppManager.getAppManager().intentToAtcivity(LogoActivity.this, MainActivity.class);			
//			}
//		}, 1000);

    }

    private void start() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQ_CODE_PERMISSION_LOCATION);
            }
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQ_CODE_PERMISSION_STORAGE);
            }
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQ_CODE_PERMISSION_PHONE);
            }

        }
        init_push();
        if (AppConfig.getFirstUsage(getApplicationContext())) {
            addShortcut();
            if (PackegeConfig.IS_BUY) {
                AppManager.getAppManager().intentToAtcivity(LogoActivity.this, GuideViewActivity.class);
            } else {
                AppManager.getAppManager().intentToAtcivity(LogoActivity.this, MainActivity.class);
            }
        } else {
            AppManager.getAppManager().intentToAtcivity(LogoActivity.this, MainActivity.class);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQ_CODE_PERMISSION_INTERNET) {
            if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                //finish();
                start();
            } else {
                start();
            }
        }
    }

    private void addShortcut() {
        Intent addShortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        // 不允许重复创建
        addShortcutIntent.putExtra("duplicate", false);// 经测试不是根据快捷方式的名字判断重复的
        String app_name = getResources().getString(R.string.app_name);
        // 名字
        addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, app_name);
        // 图标
        addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher));
        // 设置关联程序
        Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
        launcherIntent.setClass(this, MainActivity.class);
        launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);
        // 发送广播
        sendBroadcast(addShortcutIntent);
    }

    private void init_push() {
        // TODO Auto-generated method stub
        if (PackegeConfig.OPEN_GETUI)
            PushManager.getInstance().initialize(this.getApplicationContext(), null);
    }

}
